﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_DLL.Product.DAO;
using ERP_RORZE_DLL.Product.database;

namespace ERP_RORZE_PRODUCT.Product.MO
{
    public partial class frm_MOFinish : DevExpress.XtraEditors.XtraForm
    {
        ClsMODAO clsMODAO = new ClsMODAO();
        DataTable dt;
        public event EventHandler Reload;

        public frm_MOFinish()
        {
            InitializeComponent();
        }

        private void ctrlMOFinish_Load(object sender, EventArgs e)
        {
            dt = clsMODAO.GetMOFinishMONo();
            List<ClsMOTable> objs = new List<ClsMOTable>();
            foreach (DataRow r in dt.Rows)
            {
                ClsMOTable obj = new ClsMOTable();
                obj.MONo = r["MONo"].ToString();  //
                obj.PartID = r["PartID"].ToString();   ////
                obj.MOQty = int.Parse(r["MOQty"].ToString());////
                obj.DeadLine = DateTime.Parse(r["DeadLine"].ToString());     ////
                obj.Finished = (bool)r["Finished"];  ////
                obj.OKQty = (int)r["OKQty"];////
                obj.NGQty = (int)r["NGQty"];////
                obj.Finishdate = DateTime.Parse(r["Finishdate"].ToString());
                objs.Add(obj);
            }
            
            cbMONo.DataSource = objs;
            cbMONo.DisplayMember = "MONo";

            //
            //ClsPartDataDAO clsPart = new ClsPartDataDAO();
            //dt = clsPart.GetAllPartID();
            //cbPartID.DataSource = dt;
            //cbPartID.DisplayMember = "PartNo";
            //cbPartID.ValueMember = "PartNo";

        }

        private void cbMONo_SelectedIndexChanged(object sender, EventArgs e)
        {
            //
            ClsMOTable obj = ((ClsMOTable)cbMONo.SelectedItem);
            cbMONo.Text = obj.MONo.ToString();
            
            cbPartID.Text = obj.PartID.ToString();
            cbPartID.Visible = true;
            txtOKQty.Text = obj.OKQty.ToString();
            txtOKQty.Visible = true;
            txtNGQty.Text = obj.NGQty.ToString();
            txtNGQty.Visible = true;
            txtMOQty.Text = obj.MOQty.ToString();
            txtMOQty.Visible = true;
            checkFinish.Checked = obj.Finished;
            checkFinish.Visible = true;
            dateFinish.Value = obj.Finishdate;
            dateFinish.Visible = true;
            label6.Visible = true;
            label5.Visible = true;
            label7.Visible = true;
            label10.Visible = true;
            label3.Visible = true;
            label8.Visible = true;
        }

        private void btnFinish_Click(object sender, EventArgs e)
        {
            if (txtOKQtyF.Text == "")
            {
                MessageBox.Show("Ban phai nhap so luong OK! Neu khong co san pham OK ban nhap so luong bang 0");
            }
            else if (txtNGQtyF.Text == "")
            {
                MessageBox.Show("Ban phai nhap so luong NG! Neu khong co san pham NG ban nhap so luong bang 0");
            }
            else
            {
                ClsMOTable MO = new ClsMOTable();
                clsAnodQtyChangeTable AnodQtyChange = new clsAnodQtyChangeTable(); //1
                ClsDAnodInspTable DAnodInsp = new ClsDAnodInspTable();//2
                ClsDBusInspTable DBusInsp = new ClsDBusInspTable();//3
                ClsDF3StoreTable DF3Store = new ClsDF3StoreTable();//4
                ClsDHandInspTable DHandInsp = new ClsDHandInspTable();//5
                ClsBusOderTable BusOder = new ClsBusOderTable();//6
                ClsDInspAnodTable DInspAnod = new ClsDInspAnodTable();//7
                ClsDInspF3Table DInspF3 = new ClsDInspF3Table();//8
                ClsDMachineHandTable DMachineHand = new ClsDMachineHandTable();//9
                ClsDMBusInspTable DMBusInsp = new ClsDMBusInspTable();//10
                ClsDMHandInspTable DMHandInsp = new ClsDMHandInspTable();//11
                ClsDMInspBusTable DMInspBus = new ClsDMInspBusTable();//12
                ClsDMInspHandTable DMInspHand = new ClsDMInspHandTable();//13
                ClsMOOptionTable MOOption = new ClsMOOptionTable();
                
                // update MO
                MO.MONo = cbMONo.Text;
                MO.OKQty = int.Parse(txtOKQtyF.Text);
                MO.NGQty = int.Parse(txtNGQtyF.Text);
                MO.Finishdate = dateDate.Value;
                clsMODAO.UpdateMOFinish(MO);

                //Update vào các công đoạn
                //1
                AnodQtyChange.Mono = cbMONo.Text;
                clsMODAO.UpdateAnodQtyChangeMOFinish(AnodQtyChange);

                //2
                DAnodInsp.MONo = cbMONo.Text;
                clsMODAO.UpdateDAnodInspMOFinish(DAnodInsp);

                //3
                DBusInsp.MONo = cbMONo.Text;
                clsMODAO.UpdateDBusInspMOFinish(DBusInsp);

                //4
                DF3Store.MONo = cbMONo.Text;
                clsMODAO.UpdateDF3StoreMOFinish(DF3Store);

                //5
                DHandInsp.MONo = cbMONo.Text;
                clsMODAO.UpdateDHandInspMOFinish(DHandInsp);

                //6
                BusOder.MONo = cbMONo.Text;
                clsMODAO.UpdateBusOrderMOFinish(BusOder);

                //7
                DInspAnod.MONo = cbMONo.Text;
                clsMODAO.UpdateDInspAnodMOFinish(DInspAnod);

                //8
                DInspF3.MONo = cbMONo.Text;
                clsMODAO.UpdateDInspF3MOFinish(DInspF3);

                //9
                DMachineHand.MONo = cbMONo.Text;
                clsMODAO.UpdateDMachineHandMOFinish(DMachineHand);

                //10
                DMBusInsp.MONo = cbMONo.Text;
                clsMODAO.UpdateDMBusInspMOFinish(DMBusInsp);

                //11
                DMHandInsp.MONo = cbMONo.Text;
                clsMODAO.UpdateDMHandInspMOFinish(DMHandInsp);

                //12
                DMInspBus.MONo = cbMONo.Text;
                clsMODAO.UpdateDMInspBusMOFinish(DMInspBus);

                //13
                DMInspHand.MONo = cbMONo.Text;
                clsMODAO.UpdateDMInspHandMOFinish(DMInspHand);

                //update vào MOOtion
                MOOption.MONo = cbMONo.Text;
                MOOption.Finishdate = dateDate.Value;
                clsMODAO.UpdateMOOptionMOFinish(MOOption);
                Reload(sender, e);
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
